import xml.dom.minidom
import os

template="""\
<memobee>
	<specs>
		<ver>1.0</ver>
	</specs>
	<notes>
	</notes>
	<launchers>
	</launchers>
</memobee>"""
class memobee_fman:
	def set_file(self,filename,version):

		global template
		self.__file_name=filename
		template = template.replace( '1.0', str(version) )

		if  os.path.exists(self.__file_name) == False :
			if self.__file_name[-4:]!=".xml":
				self.__file_name=self.__file_name+".xml"
			
			f = open(self.__file_name,"w")
			f.write(template)
			f.close()
		
		self.xml = xml.dom.minidom.parse(self.__file_name)
		
	def save_file(self):
		f = open(self.__file_name,"w")
		f.write(self.xml.toxml())
		f.close()
		
	def add_note(self, note):
		new_note = '''
		<note>
			<id>'''+str(note.id)+'''</id>
			<title>'''+str(note.title)+'''</title>
			<date>'''+str(note.date)+'''</date>
			<body>'''+str(note.body)+'''</body>
		</note>
'''
		new_noteXML = xml.dom.minidom.parseString(new_note)
		notesXML = self.xml.getElementsByTagName('notes')
		notesXML.item(0).appendChild(new_noteXML.getElementsByTagName('note').item(0))
		self.save_file()

	def del_note(self, noteID):
		notesXML = self.xml.getElementsByTagName('notes').item(0)
		for i in notesXML.childNodes:
			if i.__class__ == xml.dom.minidom.Element:
				c_id = i.getElementsByTagName('id').item(0).childNodes[0]
				if c_id.data == noteID:
					notesXML.removeChild(i)
					self.save_file()
					break
		
	def edit_note(self, noteID, note):
		self.del_note( noteID )
		self.add_note( note )
	
	def list_note(self):
		notesXML = self.xml.getElementsByTagName('notes').item(0)
		notes_list = []
		for i in notesXML.childNodes:
			if i.__class__ == xml.dom.minidom.Element:
				c_note = init_classes.memobee_note(
						str(i.getElementsByTagName('id').item(0).childNodes[0].data),\
						str(i.getElementsByTagName('title').item(0).childNodes[0].data),\
						str(i.getElementsByTagName('date').item(0).childNodes[0].data),\
						str(i.getElementsByTagName('body').item(0).childNodes[0].data))
				notes_list += [c_note]
		return notes_list

	def del_all_notes(self):
		notes = self.list_note()
		for i in notes:
			self.del_note(i.id)
#==========================================
	def add_launcher(self, launcher):
		new_launcher = '''
		<launcher>
			<id>'''+str(launcher.id)+'''</id>
			<date>'''+str(launcher.date)+'''</date>
			<command>'''+str(launcher.command)+'''</command>
			<terminal>'''+str(int(launcher.terminal))+'''</terminal>
		</launcher>
'''
		new_launcherXML = xml.dom.minidom.parseString(new_launcher)
		launchersXML = self.xml.getElementsByTagName('launchers')
		launchersXML.item(0).appendChild(new_launcherXML.getElementsByTagName('launcher').item(0))
		self.save_file()

	def del_launcher(self, launcherID):
		launchersXML = self.xml.getElementsByTagName('launchers').item(0)
		for i in launchersXML.childNodes:
			if i.__class__ == xml.dom.minidom.Element:
				c_id = i.getElementsByTagName('id').item(0).childNodes[0]
				if c_id.data == launcherID:
					launchersXML.removeChild(i)
					self.save_file()
					break
		
	def edit_launcher(self, launcherID, launcher):
		self.del_launcher( launcherID )
		self.add_launcher( launcher )
	
	def list_launcher(self):
		launchersXML = self.xml.getElementsByTagName('launchers').item(0)
		launchers_list = []
		for i in launchersXML.childNodes:
			if i.__class__ == xml.dom.minidom.Element:
				c_launcher = init_classes.memobee_launcher(
						str(i.getElementsByTagName('id').item(0).childNodes[0].data),\
						str(i.getElementsByTagName('date').item(0).childNodes[0].data),\
						str(i.getElementsByTagName('command').item(0).childNodes[0].data),\
						str(i.getElementsByTagName('terminal').item(0).childNodes[0].data))
				launchers_list += [c_launcher]
		return launchers_list

	def del_all_launchers(self):
		launchers = self.list_launcher()
		for i in launchers:
			self.del_launcher(i.id)